//TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or
// click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter.
public class Main {
    public static void main(String[] args) {
        //1.BM2最长公共前缀
        public String longestCommonPrefix (String[] strs) {
            if(strs.length == 0) {
                return "";
            }
            int minLength = Integer.MAX_VALUE;
            for(String str : strs) {
                if(str.length() < minLength) {
                    minLength = str.length();
                }
            }
            for(int i = 0;i < minLength;i++) {
                char ch = strs[0].charAt(i);
                for(String str: strs) {
                    if(str.charAt(i) != ch) {
                        return strs[0].substring(0,i);
                    }
                }
            }
            return strs[0].substring(0,minLength);
        }
        //2.BM15二叉搜索树的最近公共祖先
        public int lowestCommonAncestor (TreeNode root, int p, int q) {
            return lowestCommonAncestorChild(root,p,q).val;
        }
        public TreeNode lowestCommonAncestorChild(TreeNode root,int p, int q) {
            if(root == null) {
                return null;
            }
            if(root.val == p || root.val == q) {
                return root;
            }
            TreeNode leftTree = lowestCommonAncestorChild(root.left,p,q);
            TreeNode rightTree = lowestCommonAncestorChild(root.right,p,q);
            if(leftTree != null && rightTree != null) {
                return root;
            } else if(leftTree != null) {
                return leftTree;
            } else {
                return rightTree;
            }
        }
    }
}